import 'dart:convert';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:talk/pages/device/x_device_router.dart';

import '../../api/DeviceApi.dart';
import '../../commponets/ItemView.dart';
import '../../home/AppLocale.dart';
import '../../routers/fluro_navigator.dart';
import '../../tools/widgets/my_app_bar.dart';
import '../provider/CustomBottomPicker.dart';
import 'model/Device.dart';


class DeviceMorePage extends StatefulWidget {
  const DeviceMorePage({
    super.key,
    this.deviceId,
    this.device,
    this.isShare,
  });

  final int? deviceId;
  final int? isShare;
  final Device? device;

  @override
  State<DeviceMorePage> createState() => _DeviceMorePageState();
}

class _DeviceMorePageState extends State<DeviceMorePage> {
  final DeviceApi _deviceApi = DeviceApi();
  late int _deviceId;
  late int _isShare;
  Device? _device;

  @override
  void initState() {
    super.initState();

    _deviceId = widget.deviceId!;
    _isShare = widget.isShare ?? 1;
    print("ddddd");
    print(_isShare);
    _device = widget.device!;
    // _fetchData();
  }

  // Future<void> _fetchData() async {
  //   try {
  //     final response = await _deviceApi.getDeviceInfo(_deviceId);
  //     if (response.code == 200) {
  //       setState(() {
  //         _device = Device.fromJson(response.data);
  //       });
  //     } else {
  //       AppLocale.text('s_500');
  //     }
  //   } catch (e) {
  //     AppLocale.text('s_get500');;
  //   }
  // }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: AppLocale.text('d_genduoshezhi'),
        backgroundColor: Colors.transparent,
        dropdownWidth: 140,
        dropdownBackgroundColor: Colors.white,
        dropdownBorderRadius: 20,
      ),



      body: Container(
          child: Column(
            children: [

              const SizedBox(height: 20,),

              // if(_isShare == 0)
                ItemView(
                  width: 400,
                  height: 50,
                  leftPadding: 10,
                  rightPadding: 20,
                  leftWidget: Row(
                    children: [
                      const SizedBox(width: 10),
                      Text(
                        AppLocale.text('d_fenxiangshebei'),
                        style: TextStyle(
                            fontSize: 15.0,
                            color: Colors.black
                        ),
                      ),
                    ],
                  ),
                  borderRadius: 8,
                  onTap: () {
                    int? userid = _device?.userId;

                    NavigatorUtils.push(
                      context,
                      '${XDeviceRouter.deviceSharePage}?deviceId=$_deviceId&userId=$userid',
                    );
                  },
                ),


              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_tongbushebeishijain'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  BrnDialogManager.showConfirmDialog(context,
                      title: AppLocale.text('d_tongbushebeishijain'),
                      cancel: AppLocale.text('cancel'),
                      confirm: AppLocale.text('confirm'),
                      message: AppLocale.text('d_tongbushebeishijaindata'),
                      onConfirm: () {
                        final toUpdate = {
                          "deviceId": _device?.deviceId,
                          "syncTime": 1,
                        };
                        _deviceApi.syncTime(toUpdate);

                        Navigator.pop(context);
                      },
                      onCancel: () {
                        Navigator.pop(context);
                      });
                },
              ),



              const SizedBox(height: 20),



              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_shiqushezhi'),
                      style: TextStyle(fontSize: 15.0, color: Colors.black),
                    ),
                  ],
                ),
                rightWidget: Row(
                  children: [
                    Row(
                      children: [
                        Text(
                          _device?.zone ?? '',
                          style: TextStyle(
                              fontSize: 14.0,
                              color: Colors.black54
                          ),
                        ),
                        const SizedBox(width: 5,),
                        const Icon(
                          Icons.arrow_forward_ios,
                          color: Colors.grey,
                          size: 14,
                        ),
                      ],
                    )
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                  // dynamic ioSelect = 1;
                  dynamic ioSelect = _device?.zoneType;

                  // 选项列表（label 是显示文案，value 是实际值）
                  List<Map<String, dynamic>> ioList = [
                    {'label': 'Asia/shanghai', 'value': 0},
                    {'label': 'Asia/beijing', 'value': 1},
                    {'label': 'Asia/nanjing', 'value': 2},
                    {'label': 'Asia/shenzhen', 'value': 3},
                  ];

                  showModalBottomSheet(
                    context: context,
                    backgroundColor: Colors.transparent,
                    builder: (BuildContext dialogContext) {
                      return CustomBottomPicker(
                        title:AppLocale.text('d_shiqushezhi'),
                        height: 300,
                        list: ioList,
                        selectedColor: Colors.blue,
                        backgroundColor: Colors.white,
                        initialValue: ioSelect,
                        //圆圈样式
                        selectedShapeType: SelectedShapeType.hollowCircle,
                        onSelect: (newValue) {
                          // 选中值变化时的回调，可在此更新页面状态
                          setState(() {
                            // 示例：如果需要把 newValue 存到 _device 或其他变量，可自行处理
                            // _device.someProperty = newValue;
                            ioSelect = newValue; // 更新假数据
                            print(ioSelect);
                          });
                        },
                      );
                    },
                  );

                },
              ),

              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_chognqi'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  BrnDialogManager.showConfirmDialog(context,
                      title: AppLocale.text('d_chognqishebei'),
                      cancel: AppLocale.text('cancel'),
                      confirm: AppLocale.text('confirm'),
                      message: AppLocale.text('d_chognqishebeidata'),
                      onConfirm: () {
                        final toUpdate = {
                          "deviceId": _device?.deviceId,
                          "reboot": 1,
                        };
                        _deviceApi.reboot(toUpdate);

                        Navigator.pop(context);
                      },
                      onCancel: () {
                        Navigator.pop(context);
                      });
                },
              ),

              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_huifuchucangshezhi'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {
                  BrnDialogManager.showConfirmDialog(context,
                      title: AppLocale.text('d_huifuchucangshezhi'),
                      cancel: AppLocale.text('cancel'),
                      confirm: AppLocale.text('confirm'),
                      message: AppLocale.text('d_huifuchucangshezhidata'),
                      onConfirm: () {
                        final toUpdate = {
                          "deviceId": _device?.deviceId,
                          "restoreFactory": 1,
                        };
                        _deviceApi.restoreFactory(toUpdate);

                        Navigator.pop(context);
                      },
                      onCancel: () {
                        Navigator.pop(context);
                      });
                },
              ),

              const SizedBox(height: 20,),


              ItemView(
                width: 400,
                height: 50,
                leftPadding: 10,
                rightPadding: 20,
                leftWidget: Row(
                  children: [
                    const SizedBox(width: 10),
                    Text(
                      AppLocale.text('d_bagzhuzhognxin'),
                      style: TextStyle(
                          fontSize: 15.0,
                          color: Colors.black
                      ),
                    ),
                  ],
                ),
                borderRadius: 8,
                onTap: () {

                },
              ),




            ],
          )),
    );
  }



}
































